package com.tianyang.yukuaifuzs.business.trade.model;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.tianyang.yukuaifuzs.business.trade.bean.AchievementOutput;
import com.tianyang.yukuaifuzs.business.trade.bean.AgentWalletOutput;
import com.tianyang.yukuaifuzs.business.trade.bean.EarningsTypeOutput;
import com.tianyang.yukuaifuzs.business.trade.bean.MerchantAchieveOutput;
import com.tianyang.yukuaifuzs.business.trade.bean.SettleBankOutput;
import com.tianyang.yukuaifuzs.business.trade.bean.SubAgentStatOutput;
import com.tianyang.yukuaifuzs.business.trade.bean.TradeFilterInput;
import com.tianyang.yukuaifuzs.business.trade.bean.WithdrawOutput;
import com.tianyang.yukuaifuzs.business.trade.bean.WithdrawRecordOutput;
import com.tianyang.yukuaifuzs.common.interfaces.CommonConfig;
import com.tianyang.yukuaifuzs.rxservice.client.PlateAgentClient;
import com.tianyang.yukuaifuzs.rxservice.client.PlateTradeClient;
import com.tianyang.yukuaifuzs.rxservice.client.PlateUserClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;

/**
 * Description：交易模块Model
 * Created by 薛勇军 on 2024/1/9.
 */
public class TradeModel {

    private static volatile TradeModel mInstance;

    private TradeModel() {
        if (mInstance != null) {
            throw new IllegalStateException("Already initialized.");
        }
    }

    public static TradeModel getInstance() {
        TradeModel result = mInstance;
        if (result == null) {
            synchronized (TradeModel.class) {
                result = mInstance;
                if (result == null) {
                    mInstance = result = new TradeModel();
                }
            }
        }
        return result;
    }

    public Observable<JSONObject> merchantTradeDetail(int pageNum, TradeFilterInput input) {
        Map<String, String> reqData = new HashMap<>();
        if (input != null) {
            reqData = JSON.parseObject(JSONObject.toJSONString(input), new TypeReference<Map<String, String>>(){});
        }
        reqData.put("pageSize", CommonConfig.PAGE_SIZE + "");
        reqData.put("pageNum", pageNum + "");
        return PlateUserClient.getInstance().merchantTradeDetail(reqData);
    }

    public Observable<JSONObject> merchantDelayTradeDetail(int pageNum) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("pageSize", CommonConfig.PAGE_SIZE + "");
        reqData.put("pageNum", pageNum + "");
        return PlateTradeClient.getInstance().merchantDelayTradeDetail(reqData);
    }

    public Observable<SettleBankOutput> queryUserSettlementCard(boolean isAgent) {
        if (isAgent)
            return PlateAgentClient.getInstance().queryUserSettlementCard(new HashMap<>());
        else
            return PlateUserClient.getInstance().queryUserSettlementCard(new HashMap<>());
    }

    public Observable<JSONObject> modifyUserSettlementCard(SettleBankOutput output, boolean isAgent) {
        Map<String, String> reqData = new HashMap<>();
        if (output != null) {
            reqData = JSON.parseObject(JSONObject.toJSONString(output), new TypeReference<Map<String, String>>(){});
        }
        if (isAgent) {
            return PlateAgentClient.getInstance().addjiesuancard(reqData);
        } else {
            return PlateUserClient.getInstance().modifyUserSettlementCard(reqData);
        }
    }

    public Observable<AgentWalletOutput> getAgentWallet() {
        return PlateTradeClient.getInstance().getAgentWallet(new HashMap<>());
    }

    public Observable<List<EarningsTypeOutput>> getSumUserWallet() {
        return PlateTradeClient.getInstance().getSumUserWallet(new HashMap<>());
    }

    public Observable<JSONObject> getUserWallet(int pageNum, int serviceType) {
        String params = "?serviceType=" +
                serviceType +
                "&pageNum=" + pageNum +
                "&pageSize=" +
                CommonConfig.PAGE_SIZE;
        return PlateTradeClient.getInstance().getUserWallet(params);
    }

    public Observable<List<WithdrawRecordOutput>> gettixianRecord(int pageNum) {
        Map<String, Object> reqData = new HashMap<>();
        reqData.put("pageSize", CommonConfig.PAGE_SIZE + "");
        reqData.put("pageNum", pageNum + "");
        reqData.put("serviceType", 2);
        return PlateTradeClient.getInstance().gettixianRecord(reqData);
    }

    public Observable<WithdrawOutput> getcantixian(int agentId) {
        Map<String, Object> reqData = new HashMap<>();
        reqData.put("agentid", agentId);
        reqData.put("serviceType", 2);
        return PlateTradeClient.getInstance().getcantixian(reqData);
    }

    public Observable<JSONObject> postixian(int agentId, String money) {
        Map<String, Object> reqData = new HashMap<>();
        reqData.put("agentid", agentId);
        reqData.put("serviceType", 2);
        reqData.put("money", money);
        return PlateTradeClient.getInstance().postixian(reqData);
    }

    public Observable<JSONObject> appAgentHomeStatQb(int pageNum, int agentId) {
        Map<String, Object> reqData = new HashMap<>();
        reqData.put("pageSize", CommonConfig.PAGE_SIZE);
        reqData.put("pageNum", pageNum + "");
        reqData.put("agentId", agentId);
        return PlateAgentClient.getInstance().appAgentHomeStatQb(reqData);
    }

    public Observable<AchievementOutput> getAgentStatHistory(String startTime, String endTime) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("startTime", startTime);
        reqData.put("endTime", endTime);
        return PlateAgentClient.getInstance().getAgentStatHistory(reqData);
    }

    public Observable<List<MerchantAchieveOutput>> posMerchantTradeTotal(
            int pageNum, String startTime, String endTime, String phone, boolean isSubMerchant) {
        Map<String, Object> reqData = new HashMap<>();
        reqData.put("pageSize", CommonConfig.PAGE_SIZE);
        reqData.put("pageNum", pageNum);
        reqData.put("startTime", startTime);
        reqData.put("endTime", endTime);
        reqData.put("phone", phone);
        if (isSubMerchant) {
            return PlateAgentClient.getInstance().subAgentStatHistoryList(reqData);
        } else {
            return PlateAgentClient.getInstance().posMerchantTradeTotal(reqData);
        }
    }

    public Observable<JSONObject> posMerchantTradeDetail(int pageNum, String startTime, String endTime, String phone) {
        Map<String, Object> reqData = new HashMap<>();
        reqData.put("pageSize", CommonConfig.PAGE_SIZE);
        reqData.put("pageNum", pageNum);
        reqData.put("startTime", startTime);
        reqData.put("endTime", endTime);
        reqData.put("phone", phone);
        return PlateAgentClient.getInstance().posMerchantTradeDetail(reqData);
    }

    public Observable<SubAgentStatOutput> subAgentStatHistoryDetail(String startTime, String endTime, String agentName, String agentPhone) {
        Map<String, Object> reqData = new HashMap<>();
        reqData.put("startTime", startTime);
        reqData.put("endTime", endTime);
        reqData.put("agentName", agentName);
        reqData.put("agentPhone", agentPhone);
        return PlateAgentClient.getInstance().subAgentStatHistoryDetail(reqData);
    }

    public Observable<JSONObject> updateMemberSettleCycle(String settleCycle, String auditTime) {
        String params = "?settleCycle=" + settleCycle;
        if (!TextUtils.isEmpty(auditTime)) {
            params = "?settleCycle=" + settleCycle + "&auditTime=" + auditTime;
        }
        return PlateTradeClient.getInstance().updateMemberSettleCycle(params);
    }
}
